<?php
/**
 * The View class
 */
class View
{
	/**
	 * The layout file
	 *
	 * @var string
	 */
	private $_layout;

	/**
	 * The response type: html, xml, json
	 *
	 * @var string
	 */
	private $_responseType;

	/**
	 * The template file
	 *
	 * @var $template
	 */
	private $_template;

	/**
	 * The page title
	 *
	 * @var $title
	 */
	private $_title;

	/**
	 * The URI array, contains: module, action, query array
	 *
	 * @var array
	 */
	public $uri;

	/**
	 * The layout directory
	 *
	 * @var string
	 */
	private $_layoutDir;

	/**
	 * The template directory
	 *
	 * @var string
	 */
	private $_templateDir;

	private function _getRenderMethodName($responseType)
	{
		return 'render' . ucfirst($responseType);   
	}
	/**
	 * Do something after subClass::__construct().
	 */
	public final function afterConstruct()
	{
		if (null === $this->getResponseType())
		{
			$this->setResponseType('html');
		}
	}

	public function render()
	{
		$renderMethod = $this->_getRenderMethodName($this->getResponseType());
		$this->$renderMethod();
	}

	/**
	 * Render the view as html.
	 *
	 * @param mixed $data
	 * @return void
	 */
	public function renderHtml()
	{
		include($this->getLayoutDir() . $this->getLayout() . '.php');
	}

	/**
	 * Get layout file
	 *
	 * @return string
	 */
	public function getLayout()
	{
		return $this->_layout;
	}

	/**
	 * Set layout file
	 *
	 * @param string $layout
	 * @return void
	 */
	public function setLayout($layout)
	{
		$this->_layout = $layout;
	}

	/**
	 * Get layout directory
	 *
	 * @return string
	 */
	public function getLayoutDir()
	{
		return $this->_layoutDir;
	}

	/**
	 * Set layout directory
	 *
	 * @param string $layoutDir
	 * @return void
	 */
	public function setLayoutDir($layoutDir)
	{
		$this->_layoutDir = $layoutDir;
	}

	/**
	 * Get response type
	 *
	 * @return string
	 */
	public function getResponseType()
	{
		return $this->_responseType;
	}

	/**
	 * Set response type
	 *
	 * @param string $responseType
	 * @return void
	 */
	public function setResponseType($responseType)
	{
		if (!empty($responseType))
		{
			if (method_exists($this, $this->_getRenderMethodName($responseType)))
			{
				$this->_responseType = $responseType;
			}
			else
			{
				DebugHelper::debug('NO_RENDER_METHOD', array('response_type' => $responseType));
			}
			
		}
	}

	/**
	 * Get template file
	 *
	 * @return string
	 */
	public function getTemplate()
	{
		return $this->_template;
	}

	/**
	 * Set template file
	 *
	 * @param string $template
	 * @return void
	 */
	public function setTemplate($template)
	{
		$this->_template = $template;
	}

	/**
	 * Get template directory
	 *
	 * @return string
	 */
	public function getTemplateDir()
	{
		return $this->_templateDir;
	}

	/**
	 * Set template directory
	 *
	 * @param string $template
	 * @return void
	 */
	public function setTemplateDir($templateDir)
	{
		$this->_templateDir = $templateDir;
	}

	/**
	 * Get page title
	 *
	 * @return string
	 */
	public function getTitle()
	{
		return $this->_title;
	}

	/**
	 * Set page title
	 *
	 * @param string $title
	 * @return void
	 */
	public function setTitle($title)
	{
		$this->_title = $title;
	}
}
